package com.taxer.service.impl;

import com.taxer.db.dao.AdminMapper;
import com.taxer.db.dao.CompanyMapper;
import com.taxer.db.entity.Admin;
import com.taxer.db.entity.Company;
import com.taxer.exception.TaxerExcepiton;
import com.taxer.service.CompanyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

@Service("companyService")
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private AdminMapper adminMapper;
    public int addCompany(Company company) throws TaxerExcepiton{
        java.util.Date  date=new java.util.Date();
        java.sql.Date  data1=new java.sql.Date(date.getTime());
        Admin admin = new Admin();
        admin.setUsername("30000");
        admin.setPassword("123456");
        admin.setStatus((byte) 3);
        admin.setCreated(data1);
        admin.setUpdated(data1);
        adminMapper.insertSelective(admin);
        Admin aa=adminMapper.selectByName("30000");
        aa.setUsername("30000"+aa.getId());
        adminMapper.updateByPrimaryKey(aa);

        company.setCreated(data1);
        company.setUpdated(data1);
        company.setAdminId(aa.getId());
        try {
            int result = 0;
            result = companyMapper.insert(company);
            return  result;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public Company findCompany(Company company) throws TaxerExcepiton {
        Company com;
        try {
            com = companyMapper.selectByPrimaryKey(company.getId());
            return com;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }

    }

    @Override
    public int deleteCompany(Company company) throws TaxerExcepiton {
        int result=0;
        try {
            result = companyMapper.deleteByPrimaryKey(company.getId());
            result = adminMapper.deleteByPrimaryKey(company.getAdminId());
            return result;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public int updateCompany(Company company) throws TaxerExcepiton {
        int result=0;
        try {
            result=companyMapper.updateByPrimaryKeySelective(company);
            return result;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public List<Company> selectAllCompany() throws TaxerExcepiton {
        List<Company> coms = new ArrayList<Company>();
        try {
            coms = companyMapper.selectAllCompany();
            return coms;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }
}
